﻿//-----------------------------------------------------------------------
// <copyright file="Favourites.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.ViewModels
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.ApplicationModel.DataTransfer;

    /// <summary>
    /// View Model for the search page.
    /// </summary>
    public class Favourites : IShare
    {
        /// <summary>
        /// Initializes a new instance of the Favourites class.
        /// </summary>
        public Favourites()
        {
            this.GoToHome = new Command(() => Navigation.Navigate(typeof(Views.TreasureMapPage)));

            this.GoBack = new Command(() =>
            {
                if (Navigation.CanGoBack)
                {
                    Navigation.GoBack();
                }
                else
                {
                    Navigation.Navigate(typeof(Views.TreasureMapPage));
                }
            });

            this.Projects = new ObservableCollection<Models.Project>();

            foreach (var favourite in Models.Favourites.GetFavourites())
            {
                if (favourite.Item1 == Models.FavouriteType.Project)
                {
                    var project = (from c in App.Database.Categories
                                   from p in c.Projects
                                   where p.Id == favourite.Item2
                                   select p).SingleOrDefault();

                    if (project != null)
                    {
                        this.Projects.Add(project);
                    }
                }
            }

            DataTransferManager.GetForCurrentView().DataRequested += this.Search_DataRequested;
        }

        /// <summary>
        /// Gets the command to go back.
        /// </summary>
        public Command GoBack { get; private set; }

        /// <summary>
        /// Gets the command to go home.
        /// </summary>
        public Command GoToHome { get; private set; }

        /// <summary>
        /// Gets the Guidance collection.
        /// </summary>
        public ObservableCollection<Models.Project> Projects { get; private set; }

        /// <summary>
        /// This navigates to the selected project and is intended to be called from the UI.
        /// </summary>
        /// <param name="selectedProject">The project to navigate to.</param>
        public void NavigateToSelectedProject(Models.Project selectedProject)
        {
            var input = Tuple.Create(selectedProject.Category, selectedProject);

            Navigation.Navigate(typeof(Views.Projects), input);
        }

        /// <summary>
        /// Event handler for a share query.
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="args">The parameter is not used.</param>
        public void Search_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            args.Request.FailWithDisplayText("Nothing can be shared on this page, but other pages do have treasure that can be shared! Happy exploring!");
        }

        /// <summary>
        /// Unregisters the share event handler.
        /// </summary>
        public void Unregister()
        {
            DataTransferManager.GetForCurrentView().DataRequested -= this.Search_DataRequested;
        }
    }
}
